package cn.iocoder.an.module.mold.controller.admin.repairstatics;

import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.repairstatics.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.repairstatics.RepairStaticsDO;
import cn.iocoder.an.module.mold.service.repairstatics.RepairStaticsService;

@Tag(name = "管理后台 - 维修统计")
@RestController
@RequestMapping("/mold/repair-statics")
@Validated
public class RepairStaticsController {

    @Resource
    private RepairStaticsService repairStaticsService;

    @PostMapping("/create")
    @Operation(summary = "创建维修统计")
    @PreAuthorize("@ss.hasPermission('mold:repair-statics:create')")
    public CommonResult<Long> createRepairStatics(@Valid @RequestBody RepairStaticsSaveReqVO createReqVO) {
        return success(repairStaticsService.createRepairStatics(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新维修统计")
    @PreAuthorize("@ss.hasPermission('mold:repair-statics:update')")
    public CommonResult<Boolean> updateRepairStatics(@Valid @RequestBody RepairStaticsSaveReqVO updateReqVO) {
        repairStaticsService.updateRepairStatics(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除维修统计")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:repair-statics:delete')")
    public CommonResult<Boolean> deleteRepairStatics(@RequestParam("id") Long id) {
        repairStaticsService.deleteRepairStatics(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除维修统计")
                @PreAuthorize("@ss.hasPermission('mold:repair-statics:delete')")
    public CommonResult<Boolean> deleteRepairStaticsList(@RequestParam("ids") List<Long> ids) {
        repairStaticsService.deleteRepairStaticsListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得维修统计")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:repair-statics:query')")
    public CommonResult<RepairStaticsRespVO> getRepairStatics(@RequestParam("id") Long id) {
        RepairStaticsDO repairStatics = repairStaticsService.getRepairStatics(id);
        return success(BeanUtils.toBean(repairStatics, RepairStaticsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得维修统计分页")
    @PreAuthorize("@ss.hasPermission('mold:repair-statics:query')")
    @TransMethodResult
    public CommonResult<PageResult<RepairStaticsRespVO>> getRepairStaticsPage(@Valid RepairStaticsPageReqVO pageReqVO) {
        PageResult<RepairStaticsDO> pageResult = repairStaticsService.getRepairStaticsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RepairStaticsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出维修统计 Excel")
    @PreAuthorize("@ss.hasPermission('mold:repair-statics:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRepairStaticsExcel(@Valid RepairStaticsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RepairStaticsDO> list = repairStaticsService.getRepairStaticsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "维修统计.xls", "数据", RepairStaticsRespVO.class,
                        BeanUtils.toBean(list, RepairStaticsRespVO.class));
    }

}